public class test2 {
    public int[] topKFrequent(int[] nums, int k) {
        HashMap<Integer,Integer> hashmap = new HashMap<>();
        for(int num:nums){
            hashmap.put(num,hashmap.getOrDefault(num,0)+1);
        }
        PriorityQueue<int[]> queue = new PriorityQueue<>((pair1,pair2)->pair2[1]-pair1[1]);
        for(HashMap.Entry<Integer,Integer> entry:hashmap.entrySet()){
            queue.offer(new int[]{entry.getKey(),entry.getValue()});
        }
        int[] res = new int[k];
        for(int i = 0;i<k;i++){
            res[i] = queue.poll()[0];
        }
        return res;
    }
}
